const { getDynamicNews } = require('../../utils/data');
Page({
  data: {
    login: true,
    banners: [
      'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/9429fa12-aeee-4b07-9e58-d3d4656838df.webp',
      'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/7cc5a411-8b4a-4e4e-8c8a-b31335db56b7.webp',
      'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/a9146b28-459a-45b2-99c5-6194d5497581.png',
      'https://omo-oss-image.thefastimg.com/portal-saas/new2023031518242917793/cms/image/544d615e-1e7a-46f8-9559-d372d462f2a7.webp'
    ],
      // 功能卡片数据，使用数组循环
      functionCards: [
        { 
          icon: '../../imgs/ticket.png', 
          label: '参观预约', 
          pageUrl: '/pages/home/ticket/ticket' },
        { icon: '../../imgs/map.png', label: '展馆地图', pageUrl: '/pages/home/map/map' },
        { icon: '../../imgs/info.png', label: '服务信息', pageUrl: '/pages/home/info/info' },
        { icon: '../../imgs/web.png', label: '官方网站', pageUrl: '/pages/home/web/web' }
      ],
    newsList: [
      { title: "博物馆新展览开幕", date: "2024-12-25" },
      { title: "纪念日活动回顾", date: "2024-12-20" },
      { title: "七三一部队历史教育讲座", date: "2024-12-15" }
    ]
  },
  onLoad(){
    this.getNews();
  },
  getNews() {
    wx.showLoading({
      title: '数据加载中...',
    });
  
    try {
      // 调用同步函数生成模拟数据
      const newsList = getDynamicNews().data; // 生成 10 条数据
      console.log(newsList)
      this.setData({ newsList });
    } catch (error) {
      console.error('数据获取失败:', error);
      wx.showToast({
        title: '数据加载失败',
        icon: 'none',
        duration: 2000,
      });
    } finally {
      wx.hideLoading(); // 隐藏加载提示
    }
  },  
  // 统一封装页面跳转逻辑
  navigateToPage(event) {
    if(!this.data.login){
      this.to();
    }
    const pageUrl = event.currentTarget.dataset.url;
    console.log(pageUrl)
    if (pageUrl) {
      wx.navigateTo({
        url: pageUrl,
        success: () => console.log('跳转到 TabBar 页面成功'),
        fail: (err) => console.error('TabBar 页面跳转失败:', err)
      });
    } else {
      wx.showToast({
        title: '页面暂未开放',
        icon: 'none',
        duration: 2000
      });
    }
  },
  //下拉刷新
  onPullDownRefresh: async function () {
    console.log("开始下拉刷新");
    await this.getNews();
    wx.stopPullDownRefresh(); // 停止下拉刷新动画
    wx.showToast({
      title: '刷新完成',
      icon: 'success',
      duration: 2000
    });
  },
  to(){
    console.log("点击")
     // 判断是否已登录
     if (!this.data.login) {
      // 如果没有登录，跳转到登录页面
      this.setData({login : true})
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
  }
});